<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="proNumber">
        <el-input
          v-model="queryParams.proNumber"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择商品状态" clearable>
          <el-option
            v-for="dict in dict.type.prod_product_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="gotoEditPage(0)"
        >聚合添加
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:product:add']"
        >新增
        </el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:product:edit']"
        >修改
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:product:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:product:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getShop"></right-toolbar>

    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商品ID" align="center" prop="proId"/>
      <el-table-column label="商品编号" align="center" prop="proNumber"/>
      <el-table-column label="价格" align="center" prop="proPrice"/>
      <el-table-column label="商品名称" align="center" prop="proName" :show-overflow-tooltip='true'/>
      <el-table-column label="商品分类ID" width="100px" align="center" prop="sortId"/>
      <el-table-column label="店铺ID" align="center" prop="shopId"/>
      <el-table-column label="商品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prod_product_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌ID" align="center" prop="brandId"/>
      <el-table-column label="首页图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="创建者" align="center" prop="createBy" />-->
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!--      <el-table-column label="更新人" align="center" prop="updateBy" />-->
      <el-table-column label="属性查看" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['product:product:edit']"
          >商品详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
          >商品图片
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:product:edit']"
          >商品参数
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <!--          @click="handleUpdate(scope.row)"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="gotoEditPage(scope.row)"
            v-hasPermi="['product:product:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:product:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getShop"
    />-->

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="商品编号" prop="proNumber">
          <el-input v-model="form.proNumber" placeholder="请输入商品编号"/>
        </el-form-item>
        <el-form-item label="价格" prop="proPrice">
          <el-input v-model="form.proPrice" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="proName">
          <el-input v-model="form.proName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品分类ID" prop="sortId">
          <el-input v-model="form.sortId" placeholder="请输入商品分类ID"/>
        </el-form-item>
        <el-form-item label="店铺ID" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺ID"/>
        </el-form-item>
        <el-form-item label="商品状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.prod_product_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌ID"/>
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      title="商品详情"
      :visible.sync="detailDrawer"
    >
      <span>{{ currentVar }}</span>
    </el-drawer>
  </div>
</template>

<script>
import {listProduct, getProduct, delProduct, addProduct, updateProduct} from "@/api/product/product";
import {getProductId, queryByShopId} from "@/api/shop/product";
import {getShopByShopUser} from "@/api/shop/shop";
import {getUserProfile} from "@/api/system/user";

export default {
  name: "Product",
  dicts: ['prod_product_status'],
  data() {
    return {
      // 控制商品详情弹窗查看
      detailDrawer: false,
      // 临时变量，用于测试
      currentVar: null,
      // 当前店铺 id
      storeId: null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proNumber: null,
        proName: null,
        shopId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proNumber: [
          {required: true, message: "商品编号不能为空", trigger: "blur"}
        ],
        proName: [
          {required: true, message: "商品名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    //获取当前登录用户的信息 //admin :a.data.userName
    this.getShop();

  },
  methods: {
    treeToList(tree) {
      tree.forEach(t => {
        t.products.forEach(p => {
          this.productList.push(p)
        });
      });
    },
    /** 查询商品列表 */
    /*getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        console.info(this.productList)
        this.total = response.total;
        this.loading = false;
      });
    },*/
    getShop() {
      this.loading = true;
      getUserProfile().then(a => {
        getShopByShopUser(a.data.userName).then(res => {
          queryByShopId(res.data.shopsId).then(b => {
            // console.info(b.rows)
            // this.productList = b.rows[0].products
            this.treeToList(b.rows)
            this.loading = false;
            this.storeId = res.data.shopsId
            // console.info(b.rows)
            // console.info(this.productList)
          })
        });
      })
    },
    // 商品详情查看
    handleDetail(row) {
      this.currentVar = row.proId;
      this.detailDrawer = true;
    },


    gotoEditPage(pro) {
      let id = pro;
      if (id !== 0) {
        id = pro.proId;
      }
      this.$tab.openPage("商品编辑", `/product/edit-product?proId=${id}&shopId=${this.storeId}`);
    },

    /** 查询商品列表 */
    /*getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        console.info("00000000000000000000000000000000000000")
        console.info(this.productList)
        this.total = response.total;
        this.loading = false;
      });
    },*/
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proId: null,
        proNumber: null,
        proPrice: null,
        proName: null,
        sortId: null,
        shopId: null,
        status: null,
        brandId: null,
        image: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        remark: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getShop();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const proId = row.proId || this.ids
      getProduct(proId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.proId != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getShop();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getShop();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const proIds = row.proId || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + proIds + '"的数据项？').then(function () {
        return delProduct(proIds);
      }).then(() => {
        this.getShop();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
